import Vue from 'vue';
import moment from 'moment';
import BrowserMatch from '@/utils/BrowserMatch.js';
Vue.filter('default_avatar', function(url) {
	if (!url) {
		return require('@/assets/icon/default-avatar.png');
	}

	return url;
});

Vue.filter('default_name', function(name) {
	if (!name) {
		return '未命名';
	}

	return name;
});

Vue.filter('url', function(value) {
	if (!value.includes('https:')) {
		return 'https:' + value;
	}

	return value;
});
Vue.filter('image_ali', function(value) {
	// const supportWebp = ['谷歌', 'UC', '火狐', 'QQ', 'Microsoft Edge'];
	const NotSupportWebp = ['Safari'];
	if (value.includes('alicdn.com') && !NotSupportWebp.includes(BrowserMatch.browser)) {
		return value + '_200x200_.webp';
	}

	return value;
});

Vue.filter('format_date', function(date, format = 'YYYY-MM-DD HH:mm:ss') {
	return date ? moment(date).format(format) : '无';
});

Vue.filter('expired_date', function(date) {
	if (!date) {
		return '已到期';
	}

	const expireTime = moment(date)
		.hour(23)
		.minute(59)
		.second(59);

	if (moment(expireTime).isBefore(moment())) {
		return '已到期';
	} else {
		return moment(expireTime).format('YYYY-MM-DD HH:mm:ss');
	}
});
